<?php

require_once( $_SERVER['DOCUMENT_ROOT'].'/../libs/smarty.inc' );

require_once( $_SERVER['DOCUMENT_ROOT'].'/../libs/smt_variables.php' );

require_once( $_SERVER['DOCUMENT_ROOT'].'/../libs/Class_DB.php' );

require_once( $_SERVER['DOCUMENT_ROOT'].'/../libs/Class_ERROR.php' );

require_once( $_SERVER['DOCUMENT_ROOT'].'/../libs/login.inc.php' );



if( !in_array( "J", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;



if( $_SERVER['REQUEST_METHOD'] == 'GET' ){

	

	$phase = 'input';

	

	if( isset($_GET['id']) && $_GET['id'] != '' ){

		

		if( is_numeric( $_GET['id'] ) ){

			

			$db = new Class_DB;



			$sql = sprintf("SELECT * FROM D_NEWS WHERE news_id = %s" , $_GET['id'] );

			$res = $db->query( $sql );

			$forms = mysql_fetch_array( $res, MYSQL_ASSOC );

			$forms['news_date'] = str_replace('-', '/', $forms['news_date'] );

		}

		

	}else{

		$forms['status'] = 1;

		$forms['link_target'] = 0;

		$forms['news_date'] = date("Y/n/d");

	}



}else{

	

	$forms = $_POST;



	if( $forms['mode'] == 'delete' ){

		//削除

		delete( $forms );

		$phase = 'complete';

		$smarty->assign('message', '削除が完了しました。');

	}





	// 入力画面から

	if( $forms['mode'] == 'input' ){

		$err = new Class_ERROR;

		

		$err_mes['news_title']	= $err->check( $forms['news_title'], array("EXIST") );

		$err_mes['news_date']	= $err->check( $forms['news_date'], array("EXIST", "YMD") );



		if( $err->clear ){

			$phase = 'confirm';

			$tran_id = sha1( microtime(true) );



			$_SESSION['TRAN_ID'] = $tran_id;

			$smarty->assign('tran_id', $tran_id );

		}else{

			$phase = 'input';

		}



	}



	// 戻るボタン

	if( $forms['mode'] == 'back' ){

		$phase = 'input';

	}



	// 確認画面から

	if( $forms['mode'] == 'confirm' ){

		

		if( $forms['news_id'] == '' ){

			//新規登録

			if( $forms['tran_id'] == $_SESSION['TRAN_ID'] ) regist( $forms );

			$phase = 'complete';

			$smarty->assign('message', '登録が完了しました。');

		}else{

			//更新

			if( $forms['tran_id'] == $_SESSION['TRAN_ID'] )	update( $forms );

			$phase = 'complete';

			$smarty->assign('message', '更新が完了しました。');

		}

		

	}



	

}







if( $phase == 'input' )		$smarty->assign('message', '登録内容を確認し、確認ボタンをクリックしてください。');

if( $phase == 'confirm' )	$smarty->assign('message', 'この内容で登録します。よろしいですか？');

( isset( $forms['news_id'] ) && $forms['news_id'] != '' ) ? $title = 'ニュース更新' : $title = 'ニュース登録';

$smarty->assign('title', $title);

$smarty->assign('arr_target', array(0	=>	'同ウインドウ/タブ', 1 =>	'別ウインドウ/タブ' ));

$smarty->assign('phase', $phase);

if( isset( $forms ) ) $smarty->assign('forms', $forms);

if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);



$smarty->display('web-admin/news_form.html');





// 新規登録

function regist( $fm ){

	

	$db = new Class_DB;



	$sql = "INSERT INTO D_NEWS(news_type, news_title, news_date, news_url, link_target, status, news_text, r_date, u_date) VALUES(" . 

"'2','" . 

$db->quote( $fm['news_title'] ) . "', '" . 

$fm['news_date'] . "', '" . 

$db->quote($fm['news_url']) . "', '" . 

$fm['link_target'] . "', '" . 

$fm['status'] . "', 'news" . 

"',CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)";



	$db->query( $sql );

	

	$_SESSION['TRAN_ID'] = '';

	

	return;

}





// 更新

function update( $fm ){

	

	$db = new Class_DB;



	$sql = "UPDATE D_NEWS SET 

news_title = '" . $db->quote( $fm['news_title'] ) . "', 

news_date = '" . $fm['news_date'] . "',

news_url = '" . $db->quote( $fm['news_url'] ) . "',

link_target = '" . $fm['link_target'] . "',

status = '" . $fm['status'] . "',

u_date = CURRENT_TIMESTAMP 

WHERE news_id = $fm[news_id]";

	

	$db->query( $sql );



	$_SESSION['TRAN_ID'] = '';



	return;

}





// 削除

function delete( $fm ){

	

	$db = new Class_DB;

	

	$sql = "DELETE FROM D_NEWS WHERE news_id = $fm[news_id]";

	

	$db->query( $sql );

	

	return;

}

?>